import axios from 'axios';
import MuseUI from 'muse-ui';
const instance = axios.create({
  baseURL: process.env.VUE_APP_SERVER_API
});

const requestConfig = config => {
  if (config.method === 'post' && !config.data) {
    config.data = {};
  }
  return config;
};

const response = config => {
  return config.data.data;
  // if (config.data.status === 0) {
  //   return config.data.data;
  // } else {
  //   MuseUI.Toast.message(config.data.msg);
  //   console.error(`axios interceptor response error: ${config.data.msg}`);
  //   return Promise.reject(new Error(config.data.msg));
  // }
};

instance.interceptors.request.use(requestConfig, error => {
  console.error(`axios interceptors request error:${error}`);
  return Promise.reject(error);
});

instance.interceptors.response.use(response, error => {
  console.error(`axios interceptors response error:${error}`);
  MuseUI.Toast.message('服务器报错！');
  return Promise.reject(error);
});

export default instance;
